﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace HobSoft.SoccerBet2014.Domain.Implementation.SqlRepository
{
    using Domain.IRepositories;
    using Domain.Models;
    using SoccerBet2014.Common.DB.Queryable;
    using SoccerBet2014.Common.Errors;

    public class BFMarketRepository : ReadWriteSqlEntRepository<BFMarket>, IBFMarketRepository
    {
        public BFMarketRepository(DbContext dbContext)
            : base(dbContext)
        {

        }

        public override BFMarket Get(params object[] id)
        {
            var market = DbSet.Find(id);
            DbContext.Entry(market).Collection(m=>m.Runners).Load();
            return market;
        }

        public override IList<BFMarket> GetMany(Expression<Func<BFMarket, bool>> predicate)
        {
            predicate.IsNull().ThrowNull("predicate", "need predicate.");
            return DbSet.Include(m => m.Runners)
                        .Where(predicate)
                        .ToList();
        }

        public override IList<BFMarket> GetMany(Expression<Func<BFMarket, bool>> predicate, params SoccerBet2014.Common.Queryable.ISortExpression<BFMarket>[] sortExpression)
        {
            predicate.IsNull().ThrowNull("predicate", "need predicate.");
            sortExpression.IsNull().ThrowNull("sortExpression", "need sortExpression.");

            return DbSet.Include(m=>m.Runners).Where(predicate).Sort(sortExpression).ToList();
        }
    }
}
